package de.ohmhochschule.bme;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class NetworkInterfacesAndIPsActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView textView1 = (TextView) findViewById(R.id.textView1);
		textView1.setText("Exernal IP:\n" + getMyExternalIP() + "\n\n"
				+ "Interfaces and IPs:" + getNetworkDevicesAndIps() + "\n\n"
				+ "All IPs:" + getAllLocalIpAddresses() + "\n"
		);
    }
    

	/**
	 * try this with
	 * - only wifi turned on: should return eth0
	 * - only 3G turned on: should return pdp0
	 * - 3G and wifi and tethering/hotspot: should return wl0.1 and pdp0
	 * 
	 * @return
	 */
	private String getNetworkDevicesAndIps() {
		String sDevices = "";
		try {

			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIPAddr = intf
						.getInetAddresses(); enumIPAddr.hasMoreElements();) {
					InetAddress iNetAddress = enumIPAddr.nextElement();
					if (!iNetAddress.isLoopbackAddress()) {
						String sLocalIP = iNetAddress.getHostAddress()
								.toString();
						String sInterfaceName = intf.getName();
						sDevices += "\n" + sInterfaceName + ": " + sLocalIP;
					}
				}
			}
		} catch (Exception ex) {
			Log.e(getLocalClassName(), ex.toString());
		}
		return sDevices;
	}
	

	private String getAllLocalIpAddresses() {
		String sIP = "";
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					sIP += "\n" + inetAddress.getHostAddress().toString();
				}
			}
		} catch (SocketException e) {
			Log.e(getLocalClassName(), e.getMessage());
		}
		return sIP;
	}

	private String getMyExternalIP() {
		String sIp = "No IP found.";

		URL url;
		try {
			url = new URL("http://androidproxy.in/what-my-android-ip.html");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					url.openStream()));
			StringBuffer webPage = new StringBuffer();
			String line;
			while (!(null == (line = br.readLine()))) {
				webPage.append(line);
			}
			br.close();
			// Log.i(getLocalClassName(), webPage.toString());

			String regexp = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})";
			Pattern pattern = Pattern.compile(regexp);
			Matcher match = pattern.matcher(webPage.toString());
			if (match.find()) {
				sIp = match.group(1);
			}

		} catch (Exception e) {
			Log.e(getLocalClassName(), e.getMessage());
		}
		return sIp;
	}
}